package cosw.com.shanghaica;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.InputFilter;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.cosw.sdkShanghaiCA.SHCAProvider;
import com.cosw.util.StringUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SignActivity extends ActionBarActivity {

    @BindView(R.id.tv_resign_data)
    TextView tvResignData;
    @BindView(R.id.et_resign_data)
    EditText etResignData;
    @BindView(R.id.tv_sign)
    TextView tvSign;
    @BindView(R.id.et_sign)
    TextView etSign;
    @BindView(R.id.tv_SHA1_confirm)
    Button tvSHA1Confirm;
    @BindView(R.id.tv_SHA256_confirm)
    Button tvSHA256Confirm;
    @BindView(R.id.tv_SM3_confirm)
    Button tvSM3Confirm;
    @BindView(R.id.tv_SHA1_verify_confirm)
    Button tvSHA1VerifyConfirm;
    @BindView(R.id.tv_SHA256_verify_confirm)
    Button tvSHA256VerifyConfirm;
    @BindView(R.id.tv_SM3_verify_confirm)
    Button tvSM3VerifyConfirm;


    private SHCAProvider provider;

    private Context mContext;

    private EditText et_input;

    private String signeddata;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign);
        ButterKnife.bind(this);

        mContext = this;

        provider = SHCAProvider.getInstance(this.getApplication());
    }

    @OnClick({R.id.tv_SHA1_confirm, R.id.tv_SHA256_confirm, R.id.tv_SM3_confirm})
    public void onSignClicked(View view) {

        switch (view.getId()) {
            case R.id.tv_SHA1_confirm: {
                et_input = new EditText(mContext);
                et_input.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
                et_input.setHint("请输入密码");
                et_input.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
                new AlertDialog.Builder(mContext, AlertDialog.THEME_HOLO_LIGHT)
                        .setTitle("请输入PIN").setIcon(android.R.drawable.ic_dialog_info)
                        .setView(et_input)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                Intent intent = getIntent();
                                int keytype = 1;
                                String containerName = "";
                                if (intent != null) {
                                    containerName = intent.getStringExtra("name");
                                } else
                                    Toast.makeText(mContext, "容器名为空", Toast.LENGTH_SHORT).show();
                                int hashtype = 1;
                                String pin = et_input.getText().toString().trim();
                                byte[] indata = StringUtil.hexStringToByteArray(etResignData.getText().toString().trim());
                                byte[] result = provider.signData(containerName, keytype, hashtype, indata, false, pin);
                                if (result != null) {
                                    String res = StringUtil.byteArrayToHexString(result);
                                    signeddata = res;
                                    etSign.setText(res);
                                } else
                                    Toast.makeText(mContext, "签名失败 ", Toast.LENGTH_LONG).show();
                            }
                        }).setNegativeButton("取消", null).show();
            }
            break;
            case R.id.tv_SHA256_confirm: {
                et_input = new EditText(mContext);
                et_input.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
                et_input.setHint("请输入密码");
                et_input.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
                new AlertDialog.Builder(mContext, AlertDialog.THEME_HOLO_LIGHT)
                        .setTitle("请输入PIN").setIcon(android.R.drawable.ic_dialog_info)
                        .setView(et_input)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                Intent intent = getIntent();
                                int keytype = 1;
                                String containerName = "";
                                if (intent != null) {
                                    containerName = intent.getStringExtra("name");
                                } else
                                    Toast.makeText(mContext, "容器名为空", Toast.LENGTH_SHORT).show();
                                int hashtype = 2;
                                String pin = et_input.getText().toString().trim();
                                byte[] indata = StringUtil.hexStringToByteArray(etResignData.getText().toString().trim());
                                byte[] result = provider.signData(containerName, keytype, hashtype, indata, false, pin);
                                if (result != null) {
                                    String res = StringUtil.byteArrayToHexString(result);
                                    signeddata = res;
                                    etSign.setText(res);
                                } else
                                    Toast.makeText(mContext, "签名失败 ", Toast.LENGTH_LONG).show();
                            }
                        }).setNegativeButton("取消", null).show();
            }
            break;
            case R.id.tv_SM3_confirm: {
                et_input = new EditText(mContext);
                et_input.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
                et_input.setHint("请输入密码");
                et_input.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
                new AlertDialog.Builder(mContext, AlertDialog.THEME_HOLO_LIGHT)
                        .setTitle("请输入PIN").setIcon(android.R.drawable.ic_dialog_info)
                        .setView(et_input)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                Intent intent = getIntent();
                                int keytype = 1;
                                String containerName = "";
                                if (intent != null) {
                                    containerName = intent.getStringExtra("name");
                                } else
                                    Toast.makeText(mContext, "容器名为空", Toast.LENGTH_SHORT).show();
                                int hashtype = 3;
                                String pin = et_input.getText().toString().trim();
                                byte[] indata = StringUtil.hexStringToByteArray(etResignData.getText().toString().trim());
                                byte[] result = provider.signData(containerName, keytype, hashtype, indata, false, pin);
                                if (result != null) {
                                    String res = StringUtil.byteArrayToHexString(result);
                                    signeddata = res;
                                    etSign.setText(res);
                                } else
                                    Toast.makeText(mContext, "签名失败 ", Toast.LENGTH_LONG).show();
                            }
                        }).setNegativeButton("取消", null).show();
            }
            break;
        }
    }

    @OnClick({R.id.tv_SHA1_verify_confirm, R.id.tv_SHA256_verify_confirm, R.id.tv_SM3_verify_confirm})
    public void onVerifyClicked(View view) {
        int hashtype = 0;
        switch (view.getId()) {
            case R.id.tv_SHA1_verify_confirm:
                hashtype = 1;
                break;
            case R.id.tv_SHA256_verify_confirm:
                hashtype = 2;
                break;
            case R.id.tv_SM3_verify_confirm:
                hashtype = 3;
                break;
        }
        Intent intent = getIntent();
        String publickey = "";
        String containername = "";
        if (intent != null) {
            publickey = intent.getStringExtra("pubkey");
            containername = intent.getStringExtra("name");
        } else
            Toast.makeText(mContext, "公钥为空", Toast.LENGTH_SHORT).show();
        byte[] indata = StringUtil.hexStringToByteArray(etResignData.getText().toString().trim());
        boolean flag = provider.verifySignedData(containername,publickey,indata,StringUtil.hexStringToByteArray(signeddata),hashtype,false);
        if (flag)
            Toast.makeText(mContext, "验签成功", Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(mContext, "验签失败", Toast.LENGTH_SHORT).show();
    }
}
